import React from "react";
import {Form, Input, Upload} from "antd";
import Button from "antd/es/button";
import {UploadOutlined} from "@ant-design/icons";
import {systemDeleteFile} from "@/util/Api";
import {commonUpload} from "@/util/FileUtil";

const GameCompanyForm = (props) => {
    let that = props.that;

    function handleChangeVal(key, e) {
        let forD = that.state.formData;
        forD[key] = e.target.value;
        that.setState({
            formData: forD
        });
    }

    function handleRemoveFile(file) {
        return new Promise((resolve, reject) => {
            systemDeleteFile({path: file.imgUrl})
                .then(res => {
                    resolve(res.success);
                })
        }).then(r => {
            if (r) {
                const removed = that.state.fileItems.filter(item => item.uid !== file.uid);
                that.setState({
                    fileItems: removed
                })
            }
        });
    }

    return (
        <>
            <Form.Item label={"名称"}
                       name={'name'}
                       rules={[{required: true, message: '名称不能为空！'}]}
            >
                <Input onChange={(e) => handleChangeVal('name', e)}
                       value={that.state.formData.name}
                       maxLength={200}
                       allowClear/>
            </Form.Item>
            <Form.Item label={"描述"}
                       name={'description'}
                       rules={[{required: true, message: '描述不能为空！'}]}
            >
                <Input.TextArea value={that.state.formData.description}
                                onChange={(e) => handleChangeVal('description', e)}
                                rows={10}
                                maxLength={1000} showCount allowClear
                />
            </Form.Item>

            <Form.Item label={"Logo"}
                       name={'imageUrl'}
                // rules={[{required: true, message: '图片不能为空！'}]}
            >
                <Upload
                    fileList={that.state.fileItems}
                    listType={'picture'}
                    maxCount={1}
                    accept=".png, .jpg, .jpeg, .bmp, .svg"
                    customRequest={(options) => commonUpload(that, options)}
                    onRemove={(file) => handleRemoveFile(file)}
                >
                    <Button icon={<UploadOutlined/>}>Upload</Button>
                </Upload>
            </Form.Item>
        </>
    )
}
export default GameCompanyForm;
